import React from "react";
import { Upload, Button, message } from "antd";
import { UploadOutlined } from "@ant-design/icons";
import type { UploadProps } from "antd";

interface FileUploadProps {
  accept?: string;
  onUpload: (file: File) => Promise<void>;
  buttonText: string;
}

const FileUpload: React.FC<FileUploadProps> = ({
  accept = ".csv",
  onUpload,
  buttonText,
}) => {
  const props: UploadProps = {
    beforeUpload: async (file) => {
      try {
        await onUpload(file);
        message.success("文件上传成功");
      } catch (error) {
        message.error("文件上传失败");
      }
      return false;
    },
    accept,
    showUploadList: false,
  };

  return (
    <Upload {...props}>
      <Button icon={<UploadOutlined />}>{buttonText}</Button>
    </Upload>
  );
};

export default FileUpload;
